import axios from 'axios'

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.baseURL = 'https://globalmailing.egoint.com'

// http请求拦截器
axios.interceptors.request.use(
    config => {
        let token = localStorage.getItem('token')
        config.headers['app-id'] = 'PA4Rr18WlcNH8ydOzIkc5WIzbsvu43z3'
        config.headers['app-key'] = 'K2dY6I6lNRY4Yc0eTdp93yE3Suq1ZM1h'
        config.headers['access-token'] = token
        if (!config.data) {
            config.data = {}
        }
        return config
    },
    err => {
        return Promise.reject(err)
    }
)

function request (opt) {
    let config = {
        method: opt.type || 'post',
        url: opt.url,
        data: opt.data || {}
    }
    if (opt.hasOwnProperty('headers')) {
        config.headers = opt.headers
    }
    return new Promise((resolve, reject) => {
        axios(config).then(data => {
            resolve(data)
        }).catch(err => {
            reject(err)
        })
    })
}

module.exports = {
    request
}